package com.yum.ms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yum.ms.entity.SeckillOrder;
import com.yum.ms.mapper.SeckillOrderMapper;
import com.yum.ms.service.ISeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yum
 * @since 2022-02-16
 */
@Service
public class SeckillOrderServiceImpl extends ServiceImpl<SeckillOrderMapper, SeckillOrder> implements ISeckillOrderService {

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     *
     * @param userId 用户id
     * @param goodsId 商品id
     * @return orderId:成功，-1:失败，0:排队中
     */
    @Override
    public String getResult(Long userId, Long goodsId) {
        SeckillOrder seckillOrder = seckillOrderMapper.selectOne(new QueryWrapper<SeckillOrder>()
                .eq("user_id", userId)
                .eq("goods_id", goodsId));
        if(seckillOrder != null) {
            return "成功";
        }else if(redisTemplate.hasKey("isStockEmpty:"+goodsId)) {
            return "失败";
        }else {
            return "排队中";
        }
    }
}
